package com.xwj.auction.controller;

import com.google.common.util.concurrent.RateLimiter;
import com.xwj.auction.service.AuctionItemService;
import com.xwj.common.dto.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author xwj
 * @date 2020/5/8
 */
@RestController
@RequestMapping
public class RobController {
    @Autowired
    private AuctionItemService auctionItemService;

    private static RateLimiter rateLimiter = RateLimiter.create(20);

    @GetMapping("/rob")
    public BaseResponse rob(Integer userId, Long id) {
//        if (!rateLimiter.tryAcquire(1000, TimeUnit.MILLISECONDS)) {
//            System.out.println("用户：" + userId + "被限流");
//            return BaseResponse.error("抢拍太火爆了，请稍候再试");
//        }
        return auctionItemService.rob(userId, id);
    }

    @GetMapping("/raise")
    public BaseResponse raise(Integer userId, Long id) {
        return auctionItemService.raise(userId, id);
    }
}
